package com.zsyh.jiujiu.wxapi;


import android.app.Activity;
import android.app.ActivityOptions;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;

import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.zsyh.jiujiu.R;
import com.zsyh.jiujiu.bean.MessageEvn;
import com.zsyh.jiujiu.utils.ToastUtils;


import org.greenrobot.eventbus.EventBus;

import java.lang.reflect.Method;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
	
	private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";
	
    private IWXAPI api;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_result);
		translucentActivity(this);
    	api = WXAPIFactory.createWXAPI(this, "wx1c1e7482e347fe25");
        api.handleIntent(getIntent(), this);
    }

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
        api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {

		Log.e("wxjieguos", " " + req.openId);
	}

	@Override
	public void onResp(BaseResp resp) {
		ToastUtils.showShort(this,resp.errCode+resp.errStr);
		Log.e("wxjieguo", "onPayFinish, errCode = " + resp.errCode+resp.errStr);


		if(resp.errCode==0){

			//showDialog("");
		}
		EventBus.getDefault().post(new MessageEvn("message", "all"));

		WXPayEntryActivity.this.finish();

		/*if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle(R.string.app_tip);
			builder.setMessage(getString(R.string.pay_result_callback_msg, String.valueOf(resp.errCode)));
			builder.show();
		}*/
	}




	private void translucentActivity(Activity activity) {

		try {
			activity.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
			activity.getWindow().getDecorView().setBackground(null);
			Method activityOptions = Activity.class.getDeclaredMethod("getActivityOptions");
			activityOptions.setAccessible(true);
			Object options = activityOptions.invoke(activity);

			Class<?>[] classes = Activity.class.getDeclaredClasses();
			Class<?> aClass = null;
			for (Class clazz : classes) {
				if (clazz.getSimpleName().contains("TranslucentConversionListener")) {
					aClass = clazz;
				}
			}
			Method method = Activity.class.getDeclaredMethod("convertToTranslucent",
					aClass, ActivityOptions.class);
			method.setAccessible(true);
			method.invoke(activity, null, options);
		} catch (Throwable t) {
			t.printStackTrace();
		}
	}
}